App.controller('runDynamicController', ['$rootScope', '$scope', '$state', 'Page', function ($rootScope, $scope, $state, Page) {


    $scope.query = {};
    $scope.query.queryType = "1"; //全部
    //$scope.query.startDate = "2017-02-01";
    //$scope.query.endDate = "2017-02-10";
    //$scope.query.notRunDay = "6";
    //var now = new Date();
    //var y = nowdate.getFullYear();
    //var m = (nowdate.getMonth() + 1) < 10 ? '0' + (nowdate.getMonth() + 1) : (nowdate.getMonth() + 1);
    //var d = nowdate.getDate() < 10 ? '0' + nowdate.getDate() : nowdate.getDate();

    function yesterday() {
        var today = new Date();
        var yesterday_milliseconds = today.getTime()-1000*60*60*24;

        var yesterday = new Date();
        yesterday.setTime(yesterday_milliseconds);

        var strYear = yesterday.getFullYear();

        var strDay = yesterday.getDate();
        var strMonth = yesterday.getMonth() + 1;

        if(strMonth<10) {
            strMonth="0" + strMonth;
        }
        var strYesterday = strYear + "-" + strMonth + "-" + strDay;
        return strYesterday
    }


    function getPreMonthTime(date) {
        var arr = date.split('-');
        var year = arr[0]; //获取当前日期的年份
        var month = arr[1]; //获取当前日期的月份
        var day = arr[2]; //获取当前日期的日
        var days = new Date(year, month, 0);
        days = days.getDate(); //获取当前日期中月的天数
        var year2 = year;
        var month2 = parseInt(month) - 1;
        if (month2 == 0) {
            year2 = parseInt(year2) - 1;
            month2 = 12;
        }
        var day2 = day;
        var days2 = new Date(year2, month2, 0);
        days2 = days2.getDate();
        if (day2 > days2) {
            day2 = days2;
        }
        if (month2 < 10) {
            month2 = '0' + month2;
        }
        var t2 = year2 + '-' + month2 + '-' + day2;
        return t2;
    }

    $scope.reset = function() {
        $scope.query = {};
        $scope.query.queryType = "1";
    }

    $scope.searchEndTime = yesterday();

    $scope.searchStartTime = getPreMonthTime($scope.searchEndTime);
    $scope.queryData = function() {
        if($scope.query.startDate == undefined || $scope.query.startDate == "") {
            Page.notice("请选择开始时间!");
            return;
        }

        if($scope.query.endDate == undefined || $scope.query.endDate == "") {
            Page.notice("请选择结束时间!");
            return;
        }
        var r = /^\+?[1-9][0-9]*$/;
        if($scope.query.notRunDay == undefined || $scope.query.notRunDay == "" ) {
            Page.notice("请填写非运营天数!");
            return;
        } else if(!r.test($scope.query.notRunDay)) {
            Page.notice("请填写正确的非运营天数!");
            return;
        }

        if($scope.query.queryType == undefined || $scope.query.queryType == "") {
            Page.notice("请选择类型!");
            return;
        }

        Page.ajaxPost('media/resource/runDynamicQuery.do',$scope.query, function(resp) {
            $scope.totalSearchList = resp.data;
            if($scope.totalSearchList.length > 0) {
                $scope.totalPages = Math.ceil($scope.totalSearchList.length / 10);
                if($scope.totalPages > 1) {
                    var showDataList = $scope.totalSearchList.slice(0,10);
                    Page.ajaxPost('media/resource/runDynamicPublish.do', {showDataList : showDataList,
                        startDate : $scope.query.startDate, endDate : $scope.query.endDate},function(resp){
                        $scope.showDataList = resp.data;
                    });
                } else if($scope.totalPages = 1) {
                    Page.ajaxPost('media/resource/runDynamicPublish.do', {showDataList : $scope.totalSearchList,
                        startDate : $scope.query.startDate, endDate : $scope.query.endDate},function(resp){
                        $scope.showDataList = resp.data;
                    });
                } else {
                    $scope.showDataList = [];
                }
                laypage({
                    cont : 'page_bar',
                    pages : $scope.totalPages,// 得到总页数
                    skin : '#3E97E4',
                    skip : true, // 是否开启跳页
                    prev : '<', // 若不显示，设置false即可
                    next : '>', // 若不显示，设置false即可
                    groups : 10, //连续分页数
                    last : $scope.totalPages,
                    curr: 1, //初始化当前页
                    jump : function(obj,first) {// 触发分页后的回调
                        /*obj是一个object类型。包括了分页的所有配置信息。
                         first一个Boolean类，检测页面是否初始加载。非常有用，可避免无限刷新。*/
                        if(!first){
                            //var param = {};
                            var tmepDataList = $scope.totalSearchList.slice((obj.curr - 1) * 10, obj.curr * 10);
                            Page.ajaxPost('media/resource/runDynamicPublish.do', {showDataList : tmepDataList,
                                startDate : $scope.query.startDate, endDate : $scope.query.endDate},function(resp){
                                $scope.showDataList = resp.data;
                            });
                        }
                    }
                })
            } else {
                Page.notice("未找到数据!");
                return;
            }


        })

    };

    $scope.clickData = function(data) {
        Page.ajaxPost('media/resource/runDynamicDetail.do',{vehicleNo : data.vehicleNo,
            startDate : $scope.query.startDate, endDate : $scope.query.endDate},function(resp){
            $scope.runDyanmicDetailList = resp.data;
            $scope.runDyanmicDetailModal.show();
        })

    };

    $scope.runDyanmicDetailModal = {

    };

    //$scope.runDynamicGrid = {
    //	options : {
    //		lazy : true
    //	},
    //    column_defs: [
		//	{
		//	    target: 1,
		//	    template: function(row) {
		//			 //return '<a ed-click="viewCustomerRes" title="查看客户资源信息">'+row.customerName+'</a>';
		//			 return row.customerName;
		//	   }
		//	 },
    //        {
    //            target: 7,
    //            buttons: [
    //                {
    //                    name: '编辑',
    //                    clazz: 'btn-edit',
    //                    icon: 'fa-edit',
    //                    event: function(row){
    //                		$scope.customer = row;
    //        				$scope.customerAddModal.show();
    //                    },
    //                    visiable: function(row) {
    //                        return row.status == '1';
    //                    }
    //                },
    //                {
    //                    name: '失效',
    //                    clazz: 'btn btn-delete',
    //                    icon: 'fa-trash-o',
    //                    event: function(row){
    //                		Page.confirm("失效后客户信息将不可用，确定继续操作?", function () {
		//		                Page.ajaxPost('customer/delete.do', row, function () {
		//		                    $scope.customerGrid.query();
		//		                });
		//		            });
    //                    },
    //                    visiable: function(row) {
    //                        return row.status == '1';
    //                    }
    //                },
    //                {
    //                    name: '设置账号',
    //                    clazz: 'btn btn-success',
    //                    icon: 'fa fa-cog',
    //                    event: function(row){
    //                		 $scope.customerUserModal.open(row);
    //                    },
    //                    visiable: function(row) {
    //                        return row.status == '1';
    //                    }
    //                },
    //                {
    //                    name: '查看',
    //                    clazz: 'btn green',
    //                    icon: 'fa-search',
    //                    event: function(row){
    //                		$scope.customerId = row.customerId;
		//		            $scope.showResoModal = true;
		//		            $scope.customerContractModal.show();
    //                    },
    //                    visiable: function(row) {
    //                        return false;
    //                    }
    //                }
    //            ]
    //        }
    //    ],
    //    add : function() {
    //        $scope.customer = {};
    //        $scope.customerAddFlag = true;
    //        $scope.customerAddModal.show();
    //    },
    //    viewCustomerRes : function(row){
    //    	 $scope.customerName = row.customerName;
    //    	 $scope.customerId = row.customerId;
    //    	 var param = {};
    //    	 param.customerId = row.customerId;
    //    	 Page.ajaxPost('customer/getContracts4Customer.do', param, function (resp) {
    //			 $scope.contracts = resp.data.contracts;
    //			 $scope.customerResourceScheduleModal.reset();
    //			 $scope.customerResourceScheduleModal.query();
    //			 $scope.customerResourceScheduleModal.show();
    //    		});
    //
    //    }
    //};





	//$scope.$on('$viewContentLoaded', function() {
	//	$scope.runDynamicGrid.query.queryType = '1';
	//});
}]);
